home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint96sb.zoo / src / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-10  |  11.5 KB  |  597 lines

  1. /*
  2. Copyright 1990,1991 Eric R. Smith. All rights reserved.
  3. */
  4.  
  5. /*
  6.  * misc. utility routines
  7.  */
  8.  
  9. #include "mint.h"
  10.  
  11. /*
  12.  * given an address, find the corresponding memory region in this program's
  13.  * memory map
  14.  */
  15.  
  16. MEMREGION *
  17. addr2mem(a)
  18.     virtaddr a;
  19. {
  20.     int i;
  21.  
  22.     for (i = 0; i < curproc->num_reg; i++) {
  23.         if (a == curproc->addr[i])
  24.             return curproc->mem[i];
  25.     }
  26.     return 0;
  27. }
  28.  
  29. /*
  30.  * given a pid, return the corresponding process
  31.  */
  32.  
  33. PROC *
  34. pid2proc(pid)
  35.     int pid;
  36. {
  37.     PROC *p;
  38.  
  39.     for (p = proclist; p; p = p->gl_next) {
  40.         if (p->pid == pid)
  41.             return p;
  42.     }
  43.     return 0;
  44. }
  45.  
  46. /*
  47.  * return a new pid
  48.  */
  49.  
  50. int
  51. newpid()
  52. {
  53.     static int _maxpid = 1;
  54.     int i;
  55. #ifndef NDEBUG
  56.     int j = 0;
  57. #endif
  58.  
  59.     do {
  60.         i = _maxpid++;
  61.         if (_maxpid >= 1000) _maxpid = 1;
  62.         assert(j++ < 1000);
  63.     } while (pid2proc(i));
  64.  
  65.     return i;
  66. }
  67.  
  68. /*
  69.  * zero out a block of memory, quickly; the block must be word-aligned,
  70.  * and should be long-aligned for speed reasons
  71.  */
  72.  
  73. void
  74. zero(place, size)
  75.     char *place;
  76.     long size;
  77. {
  78.     long cruft;
  79.  
  80.     cruft = size % 256;    /* quickzero does 256 byte blocks */
  81.     size = size / 256;
  82.     while (cruft > 0) {
  83.         *place++ = 0;
  84.         cruft--;
  85.     }
  86.     if (size > 0) {
  87.         quickzero(place, size);
  88.     }
  89. }
  90.  
  91. #ifdef JUNK_MEM
  92. void
  93. fillwjunk(place, size)
  94.     long *place;
  95.     long size;
  96. {
  97.     while (size > 0) {
  98.         *place++ = size;
  99.         size -= 4;
  100.     }
  101. }
  102. #endif
  103.  
  104. /*
  105.  * kernel memory allocation routines
  106.  */
  107.  
  108. #define KMAGIC ((MEMREGION *)0x87654321L)
  109. #define KERMEM_THRESHOLD 264
  110.  
  111. void * ARGS_ON_STACK 
  112. kmalloc(size)
  113.     long size;
  114. {
  115.     MEMREGION *m;
  116.     MEMREGION **p;
  117.  
  118.     size += sizeof(m) + sizeof (m);
  119. /*
  120.  * for small requests, we try kernel memory first, to cut down on fragmentation
  121.  */
  122.     if (size <= KERMEM_THRESHOLD)
  123.         m = get_region(ker, size);
  124.     else
  125.         m = get_region(alt, size);
  126.  
  127.     if (!m) m = get_region(core, size);
  128.     if (!m)    {
  129.         if (size <= KERMEM_THRESHOLD)
  130.             m = get_region(alt, size);
  131.         else
  132.             m = get_region(ker, size);
  133.     }
  134.     if (m) {
  135.         p = (MEMREGION **)m->loc;
  136.         *p++ = KMAGIC;
  137.         *p++ = m;
  138.         return (void *)p;
  139.     }
  140.     else {
  141.         return 0;
  142.     }
  143. }
  144.  
  145. /* allocate from ST memory only */
  146.  
  147. void *
  148. kcore(size)
  149.     long size;
  150. {
  151.     MEMREGION *m;
  152.     MEMREGION **p;
  153.  
  154.     size += sizeof(m) + sizeof (m);
  155.     m = get_region(core, size);
  156.  
  157.     if (m) {
  158.         p = (MEMREGION **)m->loc;
  159.         *p++ = KMAGIC;
  160.         *p++ = m;
  161.         return (void *)p;
  162.     }
  163.     else {
  164.         return 0;
  165.     }
  166. }
  167.  
  168. void ARGS_ON_STACK 
  169. kfree(place)
  170.     void *place;
  171. {
  172.     MEMREGION **p;
  173.     MEMREGION *m;
  174.  
  175.     if (!place) return;
  176.     p = place;
  177.     p -= 2;
  178.     if (*p++ != KMAGIC) {
  179.         FATAL("kfree: memory not allocated by kmalloc");
  180.     }
  181.     m = *p;
  182.     if (--m->links != 0) {
  183.         FATAL("kfree: block has %d links", m->links);
  184.     }
  185.     free_region(m);
  186. }
  187.  
  188. /*
  189.  * "user" memory allocation routines; the kernel can use these to
  190.  * allocate/free memory that will be attached in some way to a process
  191.  * (and freed automatically when the process exits)
  192.  */
  193. void * ARGS_ON_STACK 
  194. umalloc(size)
  195.     long size;
  196. {
  197.     return (void *)m_xalloc(size, 3);
  198. }
  199.  
  200. void ARGS_ON_STACK 
  201. ufree(block)
  202.     void *block;
  203. {
  204.     (void)m_free((virtaddr)block);
  205. }
  206.  
  207. /*
  208.  * convert a time in milliseconds to a GEMDOS style date/time
  209.  * timeptr[0] gets the time, timeptr[1] the date.
  210.  * BUG/FEATURE: in the conversion, it is assumed that all months have
  211.  * 30 days and all years have 360 days.
  212.  */
  213.  
  214. void ARGS_ON_STACK 
  215. ms_time(ms, timeptr)
  216.     ulong ms;
  217.     short *timeptr;
  218. {
  219.     ulong secs;
  220.     short tsec, tmin, thour;
  221.     short tday, tmonth, tyear;
  222.  
  223.     secs = ms / 1000;
  224.     tsec = secs % 60;
  225.     secs /= 60;        /* secs now contains # of minutes */
  226.     tmin = secs % 60;
  227.     secs /= 60;        /* secs now contains # of hours */
  228.     thour = secs % 24;
  229.     secs /= 24;        /* secs now contains # of days */
  230.     tday = secs % 30;
  231.     secs /= 30;
  232.     tmonth = secs % 12;
  233.     tyear = secs / 12;
  234.     *timeptr++ = (thour << 11) | (tmin << 5) | (tsec >> 1);
  235.     *timeptr = (tyear << 9) | ((tmonth + 1) << 5) | (tday+1);
  236. }
  237.  
  238. /*
  239.  * unixtim(time, date): convert a Dos style (time, date) pair into
  240.  * a Unix time (seconds from midnight Jan 1., 1970)
  241.  */
  242.  
  243. static int
  244. mth_start[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
  245.  
  246. long ARGS_ON_STACK 
  247. unixtim(time, date)
  248.     unsigned time, date;
  249. {
  250.     int sec, min, hour;
  251.     int mday, mon, year;
  252.     long y, s;
  253.  
  254.     sec = (time & 31) << 1;
  255.     min = (time >> 5) & 63;
  256.     hour = (time >> 11) & 31;
  257.     mday = date & 31;
  258.     mon = ((date >> 5) & 15) - 1;
  259.     year = 80 + ((date >> 9) & 255);
  260.  
  261. /* calculate tm_yday here */
  262.     y = (mday - 1) + mth_start[mon] + /* leap year correction */
  263.         ( ( (year % 4) != 0 ) ? 0 : (mon > 1) );
  264.  
  265.     s = (sec) + (min * 60L) + (hour * 3600L) +
  266.         (y * 86400L) + ((year - 70) * 31536000L) +
  267.         ((year - 69)/4) * 86400L;
  268.  
  269.     return s;
  270. }
  271.  
  272. /* convert a Unix time into a DOS time. The longword returned contains
  273.    the time word first, then the date word.
  274.    BUG: we completely ignore any time zone information.
  275. */
  276. #define SECS_PER_MIN    (60L)
  277. #define SECS_PER_HOUR   (3600L)
  278. #define SECS_PER_DAY    (86400L)
  279. #define SECS_PER_YEAR   (31536000L)
  280. #define SECS_PER_LEAPYEAR (SECS_PER_DAY + SECS_PER_YEAR)
  281.  
  282. static int
  283. days_per_mth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  284.  
  285. long ARGS_ON_STACK 
  286. dostim(t)
  287.     long t;
  288. {
  289.         unsigned long time, date;
  290.     int tm_hour, tm_min, tm_sec;
  291.     int tm_year, tm_mon, tm_mday;
  292.     int i;
  293.  
  294.     if (t <= 0) return 0;
  295.  
  296.     tm_year = 70;
  297.     while (t >= SECS_PER_YEAR) {
  298.         if ((tm_year & 0x3) == 0) {
  299.             if (t < SECS_PER_LEAPYEAR)
  300.                 break;
  301.             t -= SECS_PER_LEAPYEAR;
  302.         } else {
  303.             t -= SECS_PER_YEAR;
  304.         }
  305.         tm_year++;
  306.     }
  307.     tm_mday = (int)(t/SECS_PER_DAY);
  308.         days_per_mth[1] = (tm_year & 0x3) ? 28 : 29;
  309.         for (i = 0; tm_mday >= days_per_mth[i]; i++)
  310.                 tm_mday -= days_per_mth[i];
  311.         tm_mon = i+1;
  312.     tm_mday++;
  313.         t = t % SECS_PER_DAY;
  314.         tm_hour = (int)(t/SECS_PER_HOUR);
  315.         t = t % SECS_PER_HOUR;
  316.         tm_min = (int)(t/SECS_PER_MIN);
  317.         tm_sec = (int)(t % SECS_PER_MIN);
  318.  
  319.     if (tm_year < 80) {
  320.         tm_year = 80;
  321.         tm_mon = tm_mday = 1;
  322.         tm_hour = tm_min = tm_sec = 0;
  323.     }
  324.  
  325.     time = (tm_hour << 11) | (tm_min << 5) | (tm_sec >> 1);
  326.     date = ((tm_year - 80) & 0x7f) << 9;
  327.     date |= ((tm_mon) << 5) | (tm_mday);
  328.     return (time << 16) | date;
  329. }
  330.  
  331. /*
  332.  * Case insensitive string comparison. note that this only returns
  333.  * 0 (match) or nonzero (no match), and that the returned value
  334.  * is not a reliable indicator of any "order".
  335.  */
  336.  
  337. int ARGS_ON_STACK 
  338. strnicmp(str1, str2, len)
  339.     register const char *str1, *str2;
  340.     register int len;
  341. {
  342.     register char c1, c2;
  343.  
  344.     do {
  345.         c1 = *str1++; if (isupper(c1)) c1 = tolower(c1);
  346.         c2 = *str2++; if (isupper(c2)) c2 = tolower(c2);
  347.     } while (--len >= 0 && c1 && c1 == c2);
  348.  
  349.     if (len < 0 || c1 == c2)
  350.         return 0;
  351.     return c1 - c2;
  352. }
  353.  
  354. int ARGS_ON_STACK 
  355. stricmp(str1, str2)
  356.     const char *str1, *str2;
  357. {
  358.     return strnicmp(str1, str2, 0x7fff);
  359. }
  360.  
  361.  
  362. /*
  363.  * string utilities: strlwr() converts a string to lower case, strupr()
  364.  * converts it to upper case
  365.  */
  366.  
  367. char * ARGS_ON_STACK 
  368. strlwr(s)
  369.     char *s;
  370. {
  371.     char c;
  372.     char *old = s;
  373.  
  374.     while ((c = *s) != 0) {
  375.         if (isupper(c)) {
  376.             *s = _tolower(c);
  377.         }
  378.         s++;
  379.     }
  380.     return old;
  381. }
  382.  
  383. char * ARGS_ON_STACK 
  384. strupr(s)
  385.     char *s;
  386. {
  387.     char c;
  388.     char *old = s;
  389.  
  390.     while ((c = *s) != 0) {
  391.         if (islower(c)) {
  392.             *s = _toupper(c);
  393.         }
  394.         s++;
  395.     }
  396.     return old;
  397. }
  398.  
  399. #ifdef OWN_LIB
  400.  
  401. /*
  402.  * Case sensitive comparison functions.
  403.  */
  404.  
  405. int
  406. strncmp(str1, str2, len)
  407.     register const char *str1, *str2;
  408.     register int len;
  409. {
  410.     register char c1, c2;
  411.  
  412.     do {
  413.         c1 = *str1++;
  414.         c2 = *str2++;
  415.     } while (--len >= 0 && c1 && c1 == c2);
  416.  
  417.     if (len < 0) return 0;
  418.  
  419.     return c1 - c2;
  420. }
  421.  
  422. int
  423. strcmp(str1, str2)
  424.     const char *str1, *str2;
  425. {
  426.     register char c1, c2;
  427.  
  428.     do {
  429.         c1 = *str1++;
  430.         c2 = *str2++;
  431.     } while (c1 && c1 == c2);
  432.  
  433.     return c1 - c2;
  434. }
  435.  
  436.  
  437. /*
  438.  * some standard string functions
  439.  */
  440.  
  441. char *
  442. strcat(dst, src)
  443.     char *dst;
  444.     const char *src;
  445. {
  446.     register char *_dscan;
  447.  
  448.     for (_dscan = dst; *_dscan; _dscan++) ;
  449.     while ((*_dscan++ = *src++) != 0) ;
  450.     return dst;
  451. }
  452.  
  453. char *
  454. strcpy(dst, src)
  455.     char *dst;
  456.     const char *src;
  457. {
  458.     register char *_dscan = dst;
  459.     while ((*_dscan++ = *src++) != 0) ;
  460.     return dst;
  461. }
  462.  
  463. char *
  464. strncpy(dst, src, len)
  465.     char *dst;
  466.     const char *src;
  467.     int len;
  468. {
  469.     register char *_dscan = dst;
  470.     while (--len >= 0 && (*_dscan++ = *src++) != 0)
  471.         continue;
  472.     while (--len >= 0)
  473.         *_dscan++ = 0;
  474.     return dst;
  475. }
  476.  
  477. int
  478. strlen(scan)
  479.     const char *scan;
  480. {
  481.     register const char *_start = scan+1;
  482.     while (*scan++) ;
  483.     return (int)((long)scan - (long)_start);
  484. }
  485.  
  486. /*
  487.  * strrchr: find the last occurence of a character in a string
  488.  */
  489. char *
  490. strrchr(str, which)
  491.     const char *str;
  492.     register int which;
  493. {
  494.     register unsigned char c, *s;
  495.     register char *place;
  496.  
  497.     s = (unsigned char *)str;
  498.     place = 0;
  499.     do {
  500.         c = *s++;
  501.         if (c == which)
  502.             place = (char *)s-1;
  503.     } while (c);
  504.     return place;
  505. }
  506.  
  507. unsigned char _ctype[256] =
  508. {
  509.     _CTc, _CTc, _CTc, _CTc,                /* 0x00..0x03 */
  510.     _CTc, _CTc, _CTc, _CTc,                /* 0x04..0x07 */
  511.     _CTc, _CTc|_CTs, _CTc|_CTs, _CTc|_CTs,        /* 0x08..0x0B */
  512.     _CTc|_CTs, _CTc|_CTs, _CTc, _CTc,        /* 0x0C..0x0F */
  513.  
  514.     _CTc, _CTc, _CTc, _CTc,                /* 0x10..0x13 */
  515.     _CTc, _CTc, _CTc, _CTc,                /* 0x14..0x17 */
  516.     _CTc, _CTc, _CTc, _CTc,                /* 0x18..0x1B */
  517.     _CTc, _CTc, _CTc, _CTc,                /* 0x1C..0x1F */
  518.  
  519.     _CTs, _CTp, _CTp, _CTp,                /* 0x20..0x23 */
  520.     _CTp, _CTp, _CTp, _CTp,                /* 0x24..0x27 */
  521.     _CTp, _CTp, _CTp, _CTp,                /* 0x28..0x2B */
  522.     _CTp, _CTp, _CTp, _CTp,                /* 0x2C..0x2F */
  523.  
  524.     _CTd|_CTx, _CTd|_CTx, _CTd|_CTx, _CTd|_CTx,    /* 0x30..0x33 */
  525.     _CTd|_CTx, _CTd|_CTx, _CTd|_CTx, _CTd|_CTx,    /* 0x34..0x37 */
  526.     _CTd|_CTx, _CTd|_CTx, _CTp, _CTp,        /* 0x38..0x3B */
  527.     _CTp, _CTp, _CTp, _CTp,                /* 0x3C..0x3F */
  528.  
  529.     _CTp, _CTu|_CTx, _CTu|_CTx, _CTu|_CTx,        /* 0x40..0x43 */
  530.     _CTu|_CTx, _CTu|_CTx, _CTu|_CTx, _CTu,        /* 0x44..0x47 */
  531.     _CTu, _CTu, _CTu, _CTu,                /* 0x48..0x4B */
  532.     _CTu, _CTu, _CTu, _CTu,                /* 0x4C..0x4F */
  533.  
  534.     _CTu, _CTu, _CTu, _CTu,                /* 0x50..0x53 */
  535.     _CTu, _CTu, _CTu, _CTu,                /* 0x54..0x57 */
  536.     _CTu, _CTu, _CTu, _CTp,                /* 0x58..0x5B */
  537.     _CTp, _CTp, _CTp, _CTp,                /* 0x5C..0x5F */
  538.  
  539.     _CTp, _CTl|_CTx, _CTl|_CTx, _CTl|_CTx,        /* 0x60..0x63 */
  540.     _CTl|_CTx, _CTl|_CTx, _CTl|_CTx, _CTl,        /* 0x64..0x67 */
  541.     _CTl, _CTl, _CTl, _CTl,                /* 0x68..0x6B */
  542.     _CTl, _CTl, _CTl, _CTl,                /* 0x6C..0x6F */
  543.  
  544.     _CTl, _CTl, _CTl, _CTl,                /* 0x70..0x73 */
  545.     _CTl, _CTl, _CTl, _CTl,                /* 0x74..0x77 */
  546.     _CTl, _CTl, _CTl, _CTp,                /* 0x78..0x7B */
  547.     _CTp, _CTp, _CTp, _CTc,                /* 0x7C..0x7F */
  548.  
  549.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80..0x8F */
  550.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90..0x9F */
  551.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0..0xAF */
  552.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0..0xBF */
  553.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0..0xCF */
  554.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0..0xDF */
  555.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xE0..0xEF */
  556.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xF0..0xFF */
  557. };
  558.  
  559. int toupper(c)
  560.     int c;
  561. {
  562.     return(islower(c) ? (c ^ 0x20) : (c));
  563. }
  564.  
  565. int tolower(c)
  566.     int c;
  567. {
  568.     return(isupper(c) ? (c ^ 0x20) : (c));
  569. }
  570.  
  571. /*
  572.  * converts a decimal string to an integer
  573.  */
  574.  
  575. long
  576. atol(s)
  577.     const char *s;
  578. {
  579.     long d = 0;
  580.     int negflag = 0;
  581.     int c;
  582.  
  583.     while (*s && isspace(*s)) s++;
  584.     while (*s == '-' || *s == '+') {
  585.         if (*s == '-')
  586.             negflag ^= 1;
  587.         s++;
  588.     }
  589.     while ((c = *s++) != 0 && isdigit(c)) {
  590.         d = 10 * d + (c - '0');
  591.     }
  592.     if (negflag) d = -d;
  593.     return d;
  594. }
  595.  
  596. #endif /* OWN_LIB */
  597.